package com.kcs.fxmargin.service;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.kcs.common.utils.ConnectionUtils;
import com.kcs.common.utils.Constant;
import com.kcs.common.utils.OIDGenerator;
import com.kcs.fxmargin.dao.SetupParameterDao;
import com.kcs.fxmargin.model.MSetupParam;

public class SetupParameterService {

	SetupParameterDao dao 	= new SetupParameterDao();
	
	public MSetupParam getSetupParamter() throws Exception{
	
		Connection conn 		= null;
		MSetupParam mSetupParam = new MSetupParam();
		
		try{
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
			mSetupParam = dao.getSetupParamter(conn);
		
		} catch (Exception e){
			throw e;
		
		} finally {
			if(conn != null ) conn.close();
		}
		
		return mSetupParam;
	}
	
	public void updateSetupParameter(MSetupParam param) throws Exception {
		
		Connection conn 		= null;
		PreparedStatement pstm 	= null;
		
		try{
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
			dao.updateSetupParameter(conn, pstm, param);
			conn.commit();
		} catch (Exception e){
			conn.rollback();
			throw e;
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
		}
	}
	
	public void insertSetupParameter(MSetupParam param) throws Exception {
		
		Connection conn 		= null;
		PreparedStatement pstm 	= null;
		
		try{
			
			param.setId(OIDGenerator.create());
			
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
			dao.insertSetupParameter(conn, pstm, param);
			conn.commit();
		} catch (Exception e){
			conn.rollback();
			throw e;
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
		}
	}
	
	public void updateProcess(MSetupParam param) throws Exception {
		Connection conn 		= null;
		PreparedStatement pstm 	= null;
		
		try{
			
			param.setId(OIDGenerator.create());
			
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
			dao.updateSetupParameter(conn, pstm, param);
			
			param.setId(OIDGenerator.create());
			dao.insertSetupParameter(conn, pstm, param);
			conn.commit();
		} catch (Exception e){
			conn.rollback();
			throw e;
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
		}
	}
}
